﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Security.Cryptography;
using System.Data;
using Restaurant.DTO;
using Restaurant.DAO;

namespace Restaurant.BUS
{
    public class NhanVienService
    {
        NhanVienDB nvDAO = new NhanVienDB();
        //Ma hoa password
        public static string MaHoaMatKhau(string pass)
        {
            System.Text.Encoding enc = System.Text.Encoding.ASCII;
            byte[] myByteArray = enc.GetBytes(pass);
            return enc.GetString(SHA1.Create().ComputeHash(myByteArray));
        }

        public int ktDangNhap(string username, string pass)
        {
            DataTable dbtab = nvDAO.LayNhanVien(username, pass);
            if (dbtab.Rows.Count > 0)
            {
                DataRow dbrow= dbtab.Rows[0];
                return int.Parse(dbrow["MaLoaiNV"].ToString());
            }
            return -1;
        }

        public NhanVien LayNhanVien(string username, string pass)
        {
            DataTable dbtab = nvDAO.LayNhanVien(username, pass);
            NhanVien nv = new NhanVien();
            DataRow dbrow = dbtab.Rows[0];
            if (dbrow[0].ToString() != "")
                nv.MaNV = int.Parse(dbrow[0].ToString());
            if (dbrow[1].ToString() != "")
                nv.TenNV = dbrow[1].ToString();
            if (dbrow[2].ToString() != "")
                nv.Cmnd = dbrow[2].ToString();
            if (dbrow[3].ToString() != "")
                nv.DiaChi = dbrow[3].ToString();
            if (dbrow[4].ToString() != "")
                nv.SoDienThoai = dbrow[4].ToString();
            if (dbrow[5].ToString() != "")
                nv.NgaySinh = DateTime.Parse(dbrow[5].ToString());
            if (dbrow[6].ToString() != "")
                nv.MaLoaiNV = int.Parse(dbrow[6].ToString());
            if (dbrow[7].ToString() != "")
                nv.MaNhaHang = int.Parse(dbrow[7].ToString());
            if (dbrow[8].ToString() != "")
                nv.NgayLam = DateTime.Parse(dbrow[8].ToString());
            if (dbrow[9].ToString() != "")
                nv.Username = dbrow[9].ToString();
            if (dbrow[10].ToString() != "")
                nv.Password = dbrow[10].ToString();
            if (dbrow[11].ToString() != "")
                nv.Gioitinh = dbrow[11].ToString();
            return nv;
        }

        public DataTable LayTatCaLoaiNV()
        {
            return nvDAO.LayTatCaLoaiNV();
        }

        public DataTable LayTatCaNV()
        {
            return nvDAO.LayTatCaNV();
        }

        public string LayLoaiNV(int ma)
        {
            DataTable dbtab = nvDAO.LayLoaiNV(ma);
            DataRow dbrow = dbtab.Rows[0];
            string loainv = dbrow[1].ToString();
            return loainv;
        }

        public int LayMaLoaiNV(string loainv)
        {
            DataTable dbtab = nvDAO.LayMaLoaiNV(loainv);
            return int.Parse(dbtab.Rows[0][0].ToString());
        }

        public DataTable LayNVTheoLoai(int maloai)
        {
            DataTable dbtab = nvDAO.LayNVTheoLoai(maloai);
            return dbtab;
        }

        public DataTable TimNVTheoTen(string ten)
        {
            DataTable dbtab = nvDAO.TimNVTheoTen(ten);
            return dbtab;
        }

        public bool ThemNhanVien(NhanVien nv)
        {
            if (nv.MaNhaHang.ToString() ==""||nv.MaLoaiNV.ToString()==""||nv.TenNV == "" || nv.Cmnd == "" || nv.DiaChi == "" || nv.SoDienThoai == "" || nv.NgaySinh.ToString() == "" || nv.NgayLam.ToString() == ""||nv.Gioitinh=="")
            {
                return false;
            }
            if (nvDAO.ThemNV(nv.TenNV, nv.Cmnd, nv.DiaChi, nv.SoDienThoai, nv.NgaySinh, nv.MaLoaiNV, nv.MaNhaHang, nv.NgayLam, nv.Gioitinh))
                return true;
            return false;
        }

        public bool SuaNhanVien(NhanVien nv)
        {
            if (nv.MaNhaHang.ToString() == "" || nv.MaLoaiNV.ToString() == "" || nv.TenNV == "" || nv.Cmnd == "" || nv.DiaChi == "" || nv.SoDienThoai == "" || nv.NgaySinh.ToString() == "" || nv.NgayLam.ToString() == "" || nv.Gioitinh == "")
            {
                return false;
            }
            if (nvDAO.SuaNV(nv.MaNV, nv.TenNV, nv.Cmnd, nv.DiaChi, nv.SoDienThoai, nv.NgaySinh, nv.MaLoaiNV, nv.MaNhaHang, nv.NgayLam,nv.Gioitinh,nv.Username,nv.Password))
                return true;
            return false;
        }

        public bool XoaNhanVien(int ma)
        {
            if (nvDAO.XoaNV(ma))
                return true;
            return false;
        }

        public NhanVien TimNVID(int ma)
        {
            NhanVien nv = new NhanVien();
            DataTable dbtab = nvDAO.TimNVTheoID(ma);
            if (dbtab.Rows.Count <= 0)
            {
                nv.MaNV = -1;
                return nv;
            }
            DataRow dbrow = dbtab.Rows[0];
            nv.MaNV = int.Parse(dbrow["MaNV"].ToString());
            nv.TenNV = dbrow["TenNV"].ToString();
            nv.Cmnd = dbrow["CMND"].ToString();
            nv.DiaChi = dbrow["DiaChi"].ToString();
            nv.SoDienThoai = dbrow["SDT"].ToString();
            nv.NgaySinh = DateTime.Parse(dbrow["NgaySinh"].ToString());
            nv.MaLoaiNV = int.Parse(dbrow["MaLoaiNV"].ToString());
            nv.MaNhaHang = int.Parse(dbrow["MaNH"].ToString());
            nv.NgayLam = DateTime.Parse(dbrow["NgayLam"].ToString());
            nv.Username = dbrow["Username"].ToString();
            nv.Password = dbrow["Password"].ToString();
            nv.Gioitinh = dbrow["GioiTinh"].ToString();
            return nv;
        }

        public DataTable LoadNVDirtyreadError()
        {
            return nvDAO.LoadNVDirtyreadError();
        }

        public DataTable LoadNVDirtyread()
        {
            return nvDAO.LoadNVDirtyread();
        }

        public bool CapNhatNVDirtyreadError(NhanVien nv)
        {
            if (nv.MaNhaHang.ToString() == "" || nv.MaLoaiNV.ToString() == "" || nv.TenNV == "" || nv.Cmnd == "" || nv.DiaChi == "" || nv.SoDienThoai == "" || nv.NgaySinh.ToString() == "" || nv.NgayLam.ToString() == "" || nv.Gioitinh == "")
            {
                return false;
            }
            if (nvDAO.CapNhatNVDirtyreadError(nv.MaNV, nv.TenNV, nv.Cmnd, nv.DiaChi, nv.SoDienThoai, nv.NgaySinh, nv.MaLoaiNV, nv.MaNhaHang, nv.NgayLam, nv.Gioitinh))
                return true;
            return false;
        }

        public bool CapNhatNVDirtyread(NhanVien nv)
        {
            if (nv.MaNhaHang.ToString() == "" || nv.MaLoaiNV.ToString() == "" || nv.TenNV == "" || nv.Cmnd == "" || nv.DiaChi == "" || nv.SoDienThoai == "" || nv.NgaySinh.ToString() == "" || nv.NgayLam.ToString() == "" || nv.Gioitinh == "")
            {
                return false;
            }
            if (nvDAO.CapNhatNVDirtyread(nv.MaNV, nv.TenNV, nv.Cmnd, nv.DiaChi, nv.SoDienThoai, nv.NgaySinh, nv.MaLoaiNV, nv.MaNhaHang, nv.NgayLam, nv.Gioitinh))
                return true;
            return false;
        }

        public DataTable LoadNVUnrepeatError(ref int soluong,ref int nam,ref int nu)
        {
            return nvDAO.LoadNVUnrepeatError(ref soluong,ref nam,ref nu);
        }

        public DataTable LoadNVUnrepeat(ref int soluong, ref int nam, ref int nu)
        {
            return nvDAO.LoadNVUnrepeat(ref soluong, ref nam, ref nu);
        }

        public DataTable LoadNVPhantomError(ref int soluong, ref int nam, ref int nu)
        {
            return nvDAO.LoadNVPhantomError(ref soluong, ref nam, ref nu);
        }

        public DataTable LoadNVPhantom(ref int soluong, ref int nam, ref int nu)
        {
            return nvDAO.LoadNVPhantom(ref soluong, ref nam, ref nu);
        }

        public bool CapNhatNVLostUpdateError(NhanVien nv)
        {
            if (nv.MaNhaHang.ToString() == "" || nv.MaLoaiNV.ToString() == "" || nv.TenNV == "" || nv.Cmnd == "" || nv.DiaChi == "" || nv.SoDienThoai == "" || nv.NgaySinh.ToString() == "" || nv.NgayLam.ToString() == "" || nv.Gioitinh == "")
            {
                return false;
            }
            if (nvDAO.CapNhatNVLostUpdateError(nv.MaNV, nv.TenNV, nv.Cmnd, nv.DiaChi, nv.SoDienThoai, nv.NgaySinh, nv.MaLoaiNV, nv.MaNhaHang, nv.NgayLam, nv.Gioitinh,nv.Username,nv.Password))
                return true;
            return false;
        }

        public bool CapNhatNVLostUpdate(NhanVien nv)
        {
            if (nv.MaNhaHang.ToString() == "" || nv.MaLoaiNV.ToString() == "" || nv.TenNV == "" || nv.Cmnd == "" || nv.DiaChi == "" || nv.SoDienThoai == "" || nv.NgaySinh.ToString() == "" || nv.NgayLam.ToString() == "" || nv.Gioitinh == "")
            {
                return false;
            }
            if (nvDAO.CapNhatNVLostUpdate(nv.MaNV, nv.TenNV, nv.Cmnd, nv.DiaChi, nv.SoDienThoai, nv.NgaySinh, nv.MaLoaiNV, nv.MaNhaHang, nv.NgayLam, nv.Gioitinh,nv.Username,nv.Password))
                return true;
            return false;
        }
    }
}
